//
//  BK_OD_Count_Cell.m
//  bestkeep
//
//  Created by TOPTEAM on 2017/2/28.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BK_OD_Count_Cell.h"
#import "BK_OD_Color.h"
#import "BK_OD_Tool.h"


/**
 选择个数
 */
@implementation BK_OD_Count_Cell
{
    UIView *_BG;
    UITextField *_count;
    UIButton *_sub;
    UIButton *_add;
    UILabel *_countTitle;
    UILabel *_inventory;
    NSInteger _countLimit;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    self.separatorInset = UIEdgeInsetsMake(0, 50, 0, ApplicationBackGauge);
    
    _BG = [[UIView alloc] init];
    _BG.backgroundColor = kOrderLightGrey;
    [self.contentView addSubview:_BG];
    
    _count = [[UITextField alloc] init];
    _count.borderStyle = UITextBorderStyleNone;
    _count.delegate = self;
    
    if(ScreenHeight>600) {
        _count.font = UIFontCustom(18);
    }else{
        _count.font = UIFontCustom(15);
    }
    
    
    _count.textAlignment = NSTextAlignmentCenter;
    _count.returnKeyType = UIReturnKeyDone;
    _count.keyboardType = UIKeyboardTypeNumberPad;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
    [self.contentView addSubview:_count];
    
    
    _sub = [UIButton buttonWithType:UIButtonTypeCustom];
    [_sub addTarget:self action:@selector(subAction:) forControlEvents:UIControlEventTouchUpInside];
    [_sub setTitleColor:kOrderDrakGrey forState:UIControlStateNormal];
    _sub.titleLabel.font = UIFontCustom(26);
    [self.contentView addSubview:_sub];
    
    _add = [UIButton buttonWithType:UIButtonTypeCustom];
    [_add addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];
    [_add setTitleColor:kOrderDrakGrey forState:UIControlStateNormal];
    _add.titleLabel.textAlignment = NSTextAlignmentCenter;
    _add.titleLabel.font = UIFontCustom(25);
    [self.contentView addSubview:_add];
    
    /*
     * 数量
     */
    _countTitle = [[UILabel alloc] init];
    _countTitle.font = UIFontCustom(15);
    _countTitle.textColor=kOrderBlack1;
    [self.contentView addSubview:_countTitle];
    
    _inventory = [[UILabel alloc] init];
    _inventory.font = UIFontCustom(12);
    _inventory.textColor = kOrderLightGrey1;
    [self.contentView addSubview:_inventory];
    
    [self makeConstraints];
    
}

- (void)makeConstraints
{
    [_BG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        
        if(ScreenHeight>600) {
            make.width.mas_equalTo(140);
            make.centerX.mas_equalTo(self.contentView).offset(0);
        }else{
            make.width.mas_equalTo(110);
            make.centerX.mas_equalTo(self.contentView).offset(0);
        }
        make.height.mas_equalTo(30);
    }];
    
    [_count mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(_BG);
    }];
    
    [_sub mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.right.mas_equalTo(_BG.mas_left).offset(-5);
        make.width.height.mas_equalTo(30);
    }];
    
    [_add mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.left.mas_equalTo(_BG.mas_right).offset(5);
        make.width.height.mas_equalTo(30);
    }];
    
    [_countTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.left.mas_equalTo(self.contentView).offset(ApplicationBackGauge);
    }];
    
    [_inventory mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.mas_equalTo(self.contentView).offset(-5);
        make.right.mas_equalTo(self.contentView).offset(-ApplicationBackGauge);
        make.centerY.mas_equalTo(self.contentView);
    }];
}

- (void)subAction:(UIButton *)sender
{
    NSInteger count = [_count.text integerValue];
    count -= 1;
    
    _count.text = [NSString stringWithFormat:@"%ld", (long)count];
    
    [self textFieldDidEndEditing:_count];
}

- (void)addAction:(UIButton *)sender
{
    NSInteger count = [_count.text integerValue];
    count += 1;
    
    _count.text = [NSString stringWithFormat:@"%ld", (long)count];
    
    [self textFieldDidEndEditing:_count];
}

- (void)fillData:(id)data
{
    if (!data) {
        return;
    }
    BKODDeliveryDate *dateDataModel = data[@"deliveryDateModel"];
    _countLimit                     = [data[@"count"] integerValue];
    
    _countTitle.text = kOrderLocalizeString(@"BKOrderDDAmount");
    [_sub setImage:[UIImage imageNamed:@"BKOrder_sub_black"] forState:UIControlStateNormal];
    [_add setImage:[UIImage imageNamed:@"BKOrder_add_black"] forState:UIControlStateNormal];
    
    if (_countLimit == 0) {
        _count.text = @"0";
        _count.enabled = NO;
        _add.enabled = NO;
        _sub.enabled = NO;
    }
    else
    {
        _count.text = @"1";
        _count.enabled = YES;
        _add.enabled = YES;
        _sub.enabled = YES;
    }
    
    if ([dateDataModel.thbuyflag isEqualToString:@"0"]) {
        _inventory.hidden = NO;
        _inventory.text = [NSString stringWithFormat:@"剩余%@件", dateDataModel.amount];
    }
    else if([dateDataModel.thbuyflag isEqualToString:@"1"]){
        _inventory.hidden = YES;
    }
    
    
    
}

+(CGFloat)computeHeight:(id)data
{
//    return 72;
    return 66;
}

#pragma mark  textField delegate

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if ([_count.text isEqualToString:@""]) {
        _count.text = @"1";
    }
    
    if ([_count.text integerValue] > _countLimit) {
        NSString *msg = [NSString stringWithFormat:@"最多购买%ld件",(long)_countLimit];
        kOrderPromptMsg(msg);
        _count.text = [NSString stringWithFormat:@"%ld", (long)_countLimit];
    }
    if ([_count.text integerValue] < 1) {
        kOrderPromptMsg(@"最少选一件哦");
        _count.text = @"1";
    }
    
    if (self.delegate && [self.delegate conformsToProtocol:@protocol(BKODSelectCountCellDelegate)]) {
        [self.delegate ODSelectCountCell:self didSelectCountChanged:textField.text];
    }
}

- (void)textFieldDidChange:(NSNotification *)notification
{
    NSInteger  order_maxTextLenght = 9;
    if (_count.text.length > order_maxTextLenght) {
        kOrderPromptMsg(@"数量太多了");
        _count.text = [_count.text substringToIndex:order_maxTextLenght];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end



